package com.ruicar.afs.cloud.apply.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.apply.common.entity.ApplyBusinessConfig;
import com.ruicar.afs.cloud.apply.business.condition.ApplyBusinessConfCondition;
import com.ruicar.afs.cloud.apply.business.service.impl.ApplyBusinessConfServiceImpl;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 业务选择配置controller
 *
 * @author 耿继辉
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/businessConf")
@Api(value = "businessConf", description = "业务选择配置")
public class ApplyBusinessConfController {

    private final ApplyBusinessConfServiceImpl applyBusinessConfigService;


    /**
     * 多条件分页查询业务选择配置信息
     *
     * @param condition
     * @return
     */
    @PostMapping("/getBusinessConfInfo")
    @ApiOperation("业务选择配置-多条件分页查询")
    public IResponse<List<ApplyBusinessConfig>> getBusinessConfInfo(@RequestBody QueryCondition<ApplyBusinessConfCondition> condition) {
        return IResponse.success(applyBusinessConfigService.page(new Page(condition.getPageNumber(), condition.getPageSize()), Wrappers.<ApplyBusinessConfig>query().lambda()
                .like(ApplyBusinessConfig::getBusinessName, condition.getCondition().getBusinessName())
                .eq(StringUtils.isNotEmpty(condition.getCondition().getCarType()), ApplyBusinessConfig::getCarType, condition.getCondition().getCarType())
                .eq(StringUtils.isNotEmpty(condition.getCondition().getBusinessType()), ApplyBusinessConfig::getBusinessType, condition.getCondition().getBusinessType())
        ));
    }


    /**
     * 根据id获取信息
     *
     * @param condition
     * @return
     */
    @PostMapping("/getInfo")
    @ApiOperation("业务选择配置-根据id获取信息")
    public IResponse<List<ApplyBusinessConfig>> getInfo(@RequestBody ApplyBusinessConfCondition condition) {
        return IResponse.success(applyBusinessConfigService.getById(condition.getId()));
    }


    /**
     * 更新
     *
     * @param condition
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/updateBusinessConfInfo")
    @CacheEvict(value = "apply_business_config", allEntries = true)
    @ApiOperation(value = "更新")
    public IResponse updateBusinessConfInfo(@RequestBody ApplyBusinessConfCondition condition) {
        applyBusinessConfigService.updateById(condition);
        return IResponse.success("更新成功");
    }


    /**
     * 业务选择配置-删除
     *
     * @param condition
     * @return
     */
    @RequestMapping(value = "/deleteBusinessConfInfo", method = RequestMethod.DELETE)
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "apply_business_config", allEntries = true)
    @ApiOperation("业务选择配置-删除")
    public IResponse<ApplyBusinessConfig> deleteBusinessConfInfo(@RequestBody ApplyBusinessConfCondition condition) {
        applyBusinessConfigService.removeById(condition);
        return IResponse.success("删除成功");
    }

    /**
     * 新增
     *
     * @param condition
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/saveBusinessConfInfo")
    @CacheEvict(value = "apply_business_config", allEntries = true)
    @ApiOperation(value = "保存数据")
    public IResponse saveBusinessConfInfo(@Valid @RequestBody ApplyBusinessConfCondition condition) {
        ApplyBusinessConfig applyBusinessConfig = new ApplyBusinessConfig();
        applyBusinessConfig.setBusinessName(condition.getBusinessName());
        applyBusinessConfig.setBusinessDesc(condition.getBusinessDesc());
        applyBusinessConfig.setBusinessTypeTemp(condition.getBusinessTypeTemp());
        applyBusinessConfig.setCarType(condition.getCarType());
        applyBusinessConfig.setCarNature(condition.getCarNature());
        applyBusinessConfig.setAffiliatedWay(condition.getAffiliatedWay());
        applyBusinessConfig.setOperateWay(condition.getOperateWay());
        applyBusinessConfig.setCarPurpose(condition.getCarPurpose());
        applyBusinessConfigService.save(condition);
        return IResponse.success("保存成功");
    }
}
